package com.pk.platform.business.privilege.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pk.platform.business.privilege.dao.IRoleDao;
import com.pk.platform.business.privilege.dao.IUserDao;
import com.pk.platform.business.privilege.service.IPrivilegeService;
import com.pk.platform.business.privilege.service.IUserService;
import com.pk.platform.business.privilege.vo.UserVO;
import com.pk.platform.domain.privilege.Privilege;
import com.pk.platform.domain.privilege.Role;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.MD5;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class UserServiceImpl implements IUserService {

	private IUserDao userDao;
	
	private IRoleDao roleDao;
	
	private IPrivilegeService privilegeService;
	
	public User login(String id, String password) {
		if(!StringConverter.isNotNull(password))
		{
			return null;
		}
		String encodePassword = MD5.getMD5(password);
		User user = userDao.login(id, encodePassword);
		if(user != null)
		{
			Map<String, String> map = new HashMap<String, String>();
			if(Constant.POWER_USER.equals(id)) //如果是超级管理员,则有所有权限
			{
				List<Privilege> privilegeList = privilegeService.querySysPrivilege();
				for(Privilege privilege : privilegeList)
				{
					String url = privilege.getUrl();
					url = this.subUrl(url);
					map.put(privilege.getId().toString(), url);
				}
			}
			else //否则按角色
			{
				Set<Role> roleSet = user.getRoleSet();
				for(Role role : roleSet)
				{
					Set<Privilege> privilegeSet = role.getPrivilegeSet();
					for(Privilege privilege : privilegeSet)
					{
						String url = privilege.getUrl();
						url = this.subUrl(url);
						map.put(privilege.getId().toString(), url);
					}
				}
			}
			user.setPrivilege(map);
		}
		return user;
	}
	
	public boolean addUser(User user, String[] roleIds)
	{
		User u = userDao.get(User.class, user.getId());
		if(u == null)
		{
			String encodePassword = MD5.getMD5(user.getPassword());
			user.setPassword(encodePassword);
			userDao.add(user);
			if(roleIds != null)
			{
				String roleIdsStr = StringConverter.arrayToIntString(roleIds);
				List<Role> roleList = roleDao.queryRoleByIds(roleIdsStr);
				user.setRoleSet(new HashSet<Role>(roleList));
			}
			return true;
		}
		return false;
	}
	
	public void delUsers(String[] userIds)
	{
		for(String id : userIds)
		{
			if(!Constant.POWER_USER.equals(id))
			{
				userDao.delete(User.class, id);
			}
		}
	}
	
	public void updateUser(User user)
	{
		User u = userDao.get(User.class, user.getId());
		u.setUsername(user.getUsername());
		u.setSex(user.getSex());
		u.setDept(user.getDept());
		u.setUseType(user.getUseType());
	}
	
	public List<Map<String, Object>> queryUser(UserVO uvo, Pager pager)
	{
		return userDao.queryUser(uvo, pager);
	}
	
	public User queryUserById(String id)
	{
		return userDao.get(User.class, id);
	}
	
	public void useUsersOrNot(String[] userIds, String useType)
	{
		for(String id : userIds)
		{
			if(!Constant.POWER_USER.equals(id))
			{
				User u = userDao.get(User.class, id);
				u.setUseType(useType);
			}
		}
	}
	
	public void updatePasswordByUserId(String userId, String password)
	{
		User u = userDao.get(User.class, userId);
		String encodePassword = MD5.getMD5(password);
		u.setPassword(encodePassword);
	}
	
	public String queryUserRoleIdString(String userId)
	{
		List<Map<String, Object>> list = userDao.queryUserRoleIds(userId);
		String ids = StringConverter.listToString(list, "roleId", "");
		return ids;
	}
	
	public void assignRole(String userId, String[] roleIds)
	{
		User u = userDao.get(User.class, userId);
		String ids = StringConverter.arrayToIntString(roleIds);
		List<Role> roleList = roleDao.queryRoleByIds(ids);
		u.setRoleSet(new HashSet<Role>(roleList));
	}
	
	//不要?号
	private String subUrl(String url)
	{
		if(url != null)
		{
			int index = url.indexOf("?");
			if(index > -1)
			{
				url = url.substring(0, index);
			}
		}
		else
		{
			url = "";
		}
		return url;
	}
	
	

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IPrivilegeService getPrivilegeService() {
		return privilegeService;
	}

	public void setPrivilegeService(IPrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}

}
